package com.pan.api.config.impl;

import com.pan.api.config.ConfigUnit;
import com.pan.api.utils.PanApiDocXmlUtil;
import com.pan.api.format.*;

import com.pan.api.utils.PanReflectUtil;
import org.w3c.dom.Element;

final class PanFormatConfigUnit
    extends ConfigUnit<PanFormat>
    implements PanApiDocXmlUtil, PanReflectUtil
{
    public final static PanFormatConfigUnit INSTANCE = new PanFormatConfigUnit();
    /**
     * PanApiDoc setting file from:
     * format
     *      controller
     *      method
     *      argument
     */
    @Override
    protected PanFormat createNewObjectInstance() {
        Element formatElement = findFirst("format");
        Element controllerElement = findFirst(formatElement, "controller");
        Element methodElement = findFirst(formatElement, "method");
        Element argumentElement = findFirst(formatElement, "argument");

        String controllerFormatClassName = controllerElement.getTextContent().strip();
        String methodFormatClassName = methodElement.getTextContent().strip();
        String argumentFormatClassName = argumentElement.getTextContent().strip();

        ArgumentFormat argumentFormat = createNewInstance(argumentFormatClassName);
        MethodFormat methodFormat = createNewInstance(methodFormatClassName, argumentFormat);
        ControllerFormat controllerFormat = createNewInstance(controllerFormatClassName, methodFormat);

        PanFormat panFormat = new CommonFormat(controllerFormat);
        return panFormat;
    }
}
